package me.zhengjie.pm.projectstage.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.projectstage.domain.ProjectStage;
import me.zhengjie.pm.projectstage.service.ProjectStageService;
import me.zhengjie.pm.projectstage.service.dto.ProjectStageQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "项目阶段管理")
@RestController
@RequestMapping("/api/projectStage")
public class ProjectStageController {

    private final ProjectStageService projectStageService;

    public ProjectStageController(ProjectStageService projectStageService) {
        this.projectStageService = projectStageService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectStage:list')")
    public void download(HttpServletResponse response, ProjectStageQueryCriteria criteria) throws IOException {
        projectStageService.download(projectStageService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目阶段")
    @ApiOperation("查询项目阶段")
    @PreAuthorize("@el.check('projectStage:list')")
    public ResponseEntity<Object> getProjectStages(ProjectStageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectStageService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目阶段")
    @ApiOperation("新增项目阶段")
    @PreAuthorize("@el.check('projectStage:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectStage resources){
        return new ResponseEntity<>(projectStageService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目阶段")
    @ApiOperation("修改项目阶段")
    @PreAuthorize("@el.check('projectStage:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectStage resources){
        projectStageService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目阶段")
    @ApiOperation("删除项目阶段")
    @PreAuthorize("@el.check('projectStage:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectStageService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}